<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="外出类型" prop="type" style="width: 400px">
        <el-select v-model="pram.type"  placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.levelName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  label="起止时间" prop="leaveTime">
        <el-date-picker
          v-model="pram.leaveTime"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          size="small"
          clearable
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          align="left"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item v-if="pram.type > 1" label="前往地区" prop="content" class="dislabel">
        <el-input v-model="pram.area"   maxlength="250"  placeholder="前往地区" />
      </el-form-item>
      <el-form-item v-if=" pram.type > 1" label="事由" prop="content" class="dislabel">
        <el-input v-model="pram.reason" type="textarea"  maxlength="250" :rows="4" placeholder="事由" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:person:update','admin:person:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {Debounce} from '@/utils/validate'
import * as personApi from '@/api/person'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    editKey: {
      type: Number,
      required: true,
      default: -1,
    }
  },
  data() {
    return {
      constants:this.$constants,
      typeList:[],

      pram: {
        type: null,
        startTime: null,
        endTime: null,
        area: null,
        reason: null,
        leaveTime:null,
        typeText: null,
        days:null,
        id: null,
      },
      rules: {
        type: [{ required: true, message: '请选类型', trigger: ['blur', 'change'] }],
        leaveTime: [{ required: true, message: '请选择起止时间', trigger: ['blur', 'change'] }],
        reason: [{ required: true, message: '请填写请假事由', trigger: ['blur', 'change'] }],

      },
    }
  },
  watch:{
    'pram.startTime':{
      handler(val){
        this.pram.timeRange = val + ' -- ' + this.pram.endTime
      }
    },
    'pram.endTime':{
      handler(val){
        this.pram.timeRange = this.pram.startTime + ' -- ' + val
      }
    },
  },
  mounted() {
    this.getTypeList()
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideLeaveDialog')
    },

    onchangeStartTime (e) {
      this.pram.startTime = e;
    },
    onchangeEndTime (e) {
      this.pram.endTime = e;
    },
    getTypeList()
    {
      personApi.applyTypeApi().then(data => {
        this.typeList = data
      })
    },
    initEditData() {
      if (this.isCreate !== 1) return //编辑
      const { id, startTime, endTime, type,reason,area} = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.id = id
      this.pram.startTime = startTime
      this.pram.endTime = endTime
      this.pram.reason = reason
      this.pram.type = type
      this.pram.area = area
      if(startTime&&endTime)
      {
        this.pram.leaveTime = []
        this.pram.leaveTime.push(startTime)
        this.pram.leaveTime.push(endTime)
      }
    },
    handlerSubmit:Debounce(function(form) {
      if(this.pram.leaveTime!=null)
      {
        this.pram.startTime = this.pram.leaveTime[0];
        this.pram.endTime = this.pram.leaveTime[1];
        let startTime = new Date(this.pram.startTime).getTime();
        let endTime = new Date(this.pram.endTime).getTime();
        let days = 0;
        for (let i = startTime; i <=endTime ; i+= 24*3600*1000) {
          //判断工作日
          // let d = new Date(i);
          // if(d.getDay()>=1&& d.getDay()<=5)
          // {
          //   days++
          // }
          days++
        }
        this.pram.days = days
      }
      if(this.pram.type===1){
        this.pram.typeText = '探亲休假情况'
      }else if(this.pram.type===2)
      {
        this.pram.typeText = '事假情况'
      }else if(this.pram.type===3)
      {
        this.pram.typeText = '因公出差情况'
      }else if(this.pram.type===4)
      {
        this.pram.typeText = '节假日外出情况'
      }else if(this.pram.type===5)
      {
        this.pram.typeText = '病假情况'
      }

      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      this.$emit('assignLeaveDialog',this.pram)
    },
    handlerEdit() {

      this.$emit('assignLeaveEditDialog',this.pram,this.editKey)
    },
  }
}
</script>

<style scoped lang="scss">
  .dislabel{
    ::v-deep.el-input--small .el-input__inner,
    ::v-deep.el-input--medium .el-input__inner{
      height:36px;
      line-height:36px;
      font-size:12px;
    }
    ::v-deep.el-textarea__inner{
      font-size:12px;
    }

  }
</style>
